package book;

/**
 * 单例模式(懒汉模式)
 * @author：涂有
 * @date 2017年3月1日 下午8:22:06
 */
public class SingleInstance {

	private static SingleInstance instance;
	
	protected SingleInstance(){}
	
	synchronized public static SingleInstance getInstance(){
		
		if(instance == null){
			synchronized (SingleInstance.class) {
				if (instance == null) {
					instance = new SingleInstance();
				}
			}
		}
		return instance;
	}
	
	static class MyThread extends Thread{

		@Override
		public void run() {
			
			System.out.println(SingleInstance.getInstance().hashCode());
		}
	} 
	
	public static void main(String[] args) {
		
		Thread t1 = new MyThread();
		Thread t2 = new MyThread();
		Thread t3 = new MyThread();
		t1.start();
		t2.start();
		t3.start();
	}
	
}
